require 'net/http'
require 'rexml/document'
require 'iconv'

class NumberLocation
  
  def initialize(number)
    @number = number
    @id_xml = REXML::Document.new(open("http://www.youdao.com/smartresult-xml/search.s?type=id&q=#{@number}"))
    @mobile_xml = REXML::Document.new(open("http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=#{@number}"))
    @ip_xml = REXML::Document.new(open("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=#{ @number}"))
  end
  
  def id?
    @id_xml.elements['smartresult'].children.many? and @id_xml.elements['smartresult'].elements['product '].attributes['type'] == 'identitycard'
  end
  
  def mobile?
    @mobile_xml.elements['smartresult'].children.many? and @mobile_xml.elements['smartresult'].elements['product '].attributes['type'] == 'mobile'
  end
  
  def ip?
    @ip_xml.elements['smartresult'].children.many? and @ip_xml.elements['smartresult'].elements['product '].attributes['type'] == 'ip'
  end

  def number
    @number
  end

  #id ip mobile
  def location
    case
    when id?
       @id_xml.elements['smartresult'].elements['product '].elements['location'].text
    when mobile?
       @mobile_xml.elements['smartresult'].elements['product '].elements['location'].text
    when ip?
       @ip_xml.elements['smartresult'].elements['product '].elements['location'].text
    end
  end

  # id
  def birthday
    @id_xml.elements['smartresult'].elements['product '].elements['birthday'].text
  end

  def sex
    @id_xml.elements['smartresult'].elements['product '].elements['gender'].text == 'm' ? 'man' : 'woman'
  end
  
end